﻿using System;
using OpenQA.Selenium;
using System.Xml;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;


namespace Selenium_VS_SmapleProject
{
    public class TestConfig
    {
        private static IWebDriver _currentDriver;
        private static string _basicUrl; 
        public static IWebDriver CurrentDriver
        {
            get { return _currentDriver ?? (_currentDriver = ReadDriver("WebDriver")); }
        }

        public static string BasicUrl
        {
            get { return _basicUrl ?? (_basicUrl = GetValueFromXml("BasicUrl")); }
        }

      
        private static IWebDriver ReadDriver(string nodeName)
        {
            var driverName = GetValueFromXml(nodeName);

            switch (driverName)
            {
                case ("Firefox"):
                    {
                        _currentDriver = new FirefoxDriver();
                        break;
                    }
                case ("IExplorer"):
                    {
                        _currentDriver = new InternetExplorerDriver();
                        break;
                    }
                default:
                    {
                        throw new NotSupportedException(String.Format("'{0}' driver is not supported in tests", driverName));
                    }
                   
            }

            return _currentDriver;

        }

        private static string GetValueFromXml(string nodeName)
        {
            var config = new XmlDocument();
            config.Load("TestConfig.xml");

            var value = config.SelectSingleNode(@"//TestConfig/" + nodeName);

            return value!=null ? value.InnerText : "";
        }
    }
}
